package com.seekting.mynews.connect.baidu;

import android.content.Context;
import android.os.Bundle;

import com.alibaba.fastjson.JSONObject;
import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import com.seekting.mynews.R;
import com.seekting.mynews.connect.Connector;
import com.seekting.mynews.connect.baidu.bean.social.BaiduSocialNewsGroup;
import com.seekting.mynews.manager.Action;
import com.seekting.util.SLog;

import java.util.HashMap;
import java.util.Map;

public class BaiduNewsConnector extends Connector {

    private String key;
    private Response.Listener<String> listener;
    private Map<String, String> header;

    public BaiduNewsConnector(RequestQueue reques, Context context) {
        super(reques, context);
        key = context.getResources().getString(R.string.baidu_news_key);
        header = new HashMap<String, String>();
        key = mContext.getString(R.string.baidu_news_key);
        header.put("apikey", key);
        listener = new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                JSONObject jsonObject = JSONObject.parseObject(response);
                BaiduSocialNewsGroup baiduSocialNewsGroup = JSONObject.toJavaObject(jsonObject,
                        BaiduSocialNewsGroup.class);
                SLog.d(baiduSocialNewsGroup);

            }
        };
    }

    @Override
    public String reqeust(Action action, Bundle extra) {
        String url = mContext.getString(R.string.baidu_social_url);
        StringRequest stringRequest = new StringRequest(url, listener, null) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return header;
            }

        };
        mRequestQueue.add(stringRequest);
        return null;
    }
}
